<?php 
class annonce_envoyer extends MX_Controller{
	public function mail($id=''){
		
		if($this->input->post('submit')) self::envoyer_annonce();
		
		$anonnceInfo= $this->load->Module('product/show');
		$keywords=$description=$title=$annoce='';
		if(!is_numeric($id))
			$id=$this->input->post('id');
		if(is_numeric($id)){
			$annoce=$anonnceInfo->get_info($id);		
			$title="Envoyer annonce ".$annoce->prd_name;
			$description=" Vous êtes intéressé par l'annonce ".$annoce->prd_name;
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'annonce'=>$annoce,				
				'filter'=>''
		);

		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		
		
		$this->view(
				array('header',
					'annonce-envoyer-mail',
					'annonce-info',
					'footer'), 
				$data); // Display the page
	}
	public function envoyer_annonce(){

		
		$setting=$this->load->Module('setting/data');
		$anonnceInfo= $this->load->Module('product/show');
		$annonce=$anonnceInfo->get_info($this->input->post('id'));
		
		if(!$annonce){
			$data=array(
					'title'=>"Cette annonce est désactivée",
					'description'=>"Annonce non-trouvable",
					'keywords'=>''
			);
				
			$data=$setting->data($data);
			$this->view(
					array('header',
							'annonce-desactive',
							'footer'),
									$data); // Display the page
							return false;
				}
				
				$option=$this->load->module('setting/option');
				$annonce->option=$option->getOptions($annonce->category_id,$annonce->option,true);
				//send email
				$email= $this->load->Module('send/email');
				//$settings=$this->load->Module('setting/admin');
				$edition=$this->load->Module('setting/edition');
				//$setting_admin=$settings->get_admin();
				$edition_send=$edition->get_Edition('contact_annonce');
				
				$from_email=$this->input->post('email');
				$from_name=$this->input->post('name');
				$to_email=$annonce->user_email;
				$subject="Vous avez été contacté(e) pour votre annonce \"{$annonce->prd_name}\"";
				$message=$edition_send[0]->setting;
				$message_data=(array)$annonce;
				$message_data['from_email']=$from_email;
				$message_data['from_name']=$from_name;
				$message_data['phone']=$this->input->post('telephone');
				$message_data['link']=$this->config->config['base_url'].$annonce->link_category.'/'.$annonce->prd_link.'.html';
				
				$message_data['phishing']=$this->config->config['base_url'].'phishing.html';
				
				foreach ($message_data as $key=>$data){
					if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
				}
				
				$message=strip_tags($this->input->post('message')).$message;
				
				$status='send';
				$action='send_to_seller';
				$description='';
				
				$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
				
				$data=array(
						'title'=>"Conseiller l'annonce à un ami",
						'description'=>"Conseiller l'annonce à un ami",
						'keywords'=>"Conseiller annonce ami, envoyer annonce amin",
						'filter'=>'',
						'annonce'=>$annonce
				);
				
				$data=$setting->data($data);
				$this->view(
						array('header',
								'annonce-envoyer-message',
								'annonce-info',
								'footer'),
						$data); // Display the page
			}
		

}